# Author: D.S. Hansen & Gijs Schumacher
# Email: g.schumacher@uva.nl
# Purpose: Produce plots for congress speech paper

# Last updated: February 2019

# Load data and packages ---- 
rm(list=ls())
library(ggplot2)
library(plyr)
library(ggthemes)
library(ggpubr)
library(stargazer)
sessionInfo()
options(scipen = 999)

load("dutch.speeches.analysis.Rdata")
load("danish.speeches.analysis.Rdata")
danish.speeches.analysis$party2 <- danish.speeches.analysis$party
data <- data.frame(rbind(dutch.speeches.analysis, danish.speeches.analysis))

# Emotion -----------------------------------------------------------------
polarity.nl <- lm(polarity ~ scale(rile) + scale(abs(rile)) + scale(year) + scale(seat.share) + partyleader + office + women, data=dutch.speeches.analysis)
polarity.dk <- lm(polarity ~ scale(rile) + scale(abs(rile)) + scale(year) + scale(seat.share) + partyleader + office + women, data=danish.speeches.analysis)
arousal.nl <- lm(arousal ~ scale(rile) + scale(abs(rile)) + scale(year) + scale(seat.share) + partyleader + office + women, data=dutch.speeches.analysis)
arousal.dk <- lm(arousal ~ scale(rile) + scale(abs(rile)) + scale(year) + scale(seat.share) + partyleader + office + women, data=danish.speeches.analysis)

covariates <- c("Left-Right", "Absolute Left-Right", "Year", "Seat share", "Party leader", "In government", "Women")
stargazer(polarity.nl, polarity.dk, arousal.nl, arousal.dk,covariate.labels=covariates, column.labels=c("NL", "DK", "NL", "DK"), out="emotion.htm")

emotions.time <- ddply(data, .(fiveyear2, country), summarize,
                  mean.polarity = mean(polarity, na.rm=TRUE),
                  mean.arousal = mean(arousal, na.rm=TRUE),
                  se.polarity = sd(polarity, na.rm=TRUE) / sqrt(length(polarity)),
                  se.arousal = sd(arousal, na.rm=TRUE) / sqrt(length(arousal)),
                  lo.polarity = mean.polarity - 1.96*se.polarity,
                  hi.polarity = mean.polarity + 1.96*se.polarity, 
                  lo.arousal = mean.arousal - 1.96*se.arousal,
                  hi.arousal = mean.arousal + 1.96*se.arousal)
emotions.time$country <- ifelse(emotions.time$country=="DK", "Denmark", "Netherlands")

polarity.time <- ggplot(data=emotions.time, aes(x=fiveyear2, y=mean.polarity, group=country, color=country)) +
  geom_ribbon(data=emotions.time, aes(x=fiveyear2, ymin=lo.polarity, ymax=hi.polarity, fill=country, group=country, color=country, linetype=NA)) +
  facet_grid(country~.) +
  theme_hc() +
  scale_fill_manual(values=c("red", "blue"), name="fill") +
  ggtitle("A. Confidence region polarity over time") +
  xlab("5 year period") +
  ylab("Polarity (positive-negative/arousal)") +
  theme(legend.position = "off")

#ggsave(polarity.time, file="Plots/polarity.time.jpg", dpi=900)

arousal.time <- ggplot(data=emotions.time, aes(x=fiveyear2, y=mean.arousal, group=country, color=country)) +
  geom_ribbon(data=emotions.time, aes(x=fiveyear2, ymin=lo.arousal, ymax=hi.arousal, fill=country, group=country, color=country, linetype=NA)) +
  facet_grid(country~.) +
  theme_hc() +
  scale_fill_manual(values=c("red", "blue"), name="fill") +
  ggtitle("B. Confidence region arousal over time") +
  xlab("5 year period") +
  ylab("Arousal (positive + negative)") +
  theme(legend.position = "off")

#ggsave(arousal.time, file="Plots/polarity.time.jpg", dpi=900)

emotions.party <- ddply(data, .(party2, country), summarize,
                       mean.polarity = mean(polarity, na.rm=TRUE),
                       mean.arousal = mean(arousal, na.rm=TRUE),
                       se.polarity = sd(polarity, na.rm=TRUE) / sqrt(length(polarity)),
                       se.arousal = sd(arousal, na.rm=TRUE) / sqrt(length(arousal)),
                       lo.polarity = mean.polarity - 1.96*se.polarity,
                       hi.polarity = mean.polarity + 1.96*se.polarity, 
                       lo.arousal = mean.arousal - 1.96*se.arousal,
                       hi.arousal = mean.arousal + 1.96*se.arousal)
emotions.party$country <- ifelse(emotions.party$country=="DK", "Denmark", "Netherlands")
emotions.party <- emotions.party[-which(emotions.party$party2=="CPN"),]
emotions.party$party2 <- ifelse(emotions.party$party2=="Groenlinks", "GL",
                         ifelse(emotions.party$party2=="SF","F",
                         ifelse(emotions.party$party2=="S","A",
                         ifelse(emotions.party$party2=="EL","�",
                         ifelse(emotions.party$party2=="DF","O",emotions.party$party2)))))


polarity.party <- ggplot(data=emotions.party, aes(x=party2, y=mean.polarity)) +
  geom_bar(stat="identity", alpha=.1) +
  geom_errorbar(data=emotions.party, aes(x=party2, ymin=lo.polarity, ymax=hi.polarity, width=.2)) +
  facet_grid(country~., scales="free", space="free") +
  theme_hc() +
  #scale_fill_manual(values=c("red", "blue"), name="fill") +
  ggtitle("C. Mean polarity per party") +
  ylab("Polarity (Positive - Negative / Arousal)") +
  xlab("") +
  theme(legend.position = "off") +
  coord_flip()

#ggsave(polarity.party, file="Plots/polarity.party.jpg", dpi=900)

arousal.party <- ggplot(data=emotions.party, aes(x=party2, y=mean.arousal)) +
  geom_bar(stat="identity", alpha=.1) +
  geom_errorbar(data=emotions.party, aes(x=party2, ymin=lo.arousal, ymax=hi.arousal, width=.2)) +
  facet_grid(country~., scales="free", space="free") +
  theme_hc() +
  ggtitle("D. Mean arousal per party") +
  ylab("Arousal (positive + negative)") +
  xlab("") +
  theme(legend.position = "off") +
  coord_flip()

#ggsave(arousal.time, file="Plots/arousal.party.jpg", dpi=900)

emotions <- ggarrange(polarity.time, arousal.time, polarity.party, arousal.party, ncol=2,nrow=2)
ggsave(emotions, file="emotions.jpg", dpi=900, width=9, height=6)


